package com.e3.sysmg.security.server;

import java.util.List;

import com.e3.core.base.ServiceException;
import com.e3.core.base.ServiceLocator;
import com.e3.sysmg.security.client.RoleRPCService;
import com.e3.sysmg.security.model.ModuleModel;
import com.e3.sysmg.security.model.RoleModel;
import com.e3.sysmg.security.model.RoleQC;
import com.e3.sysmg.security.service.SecurityQueryService;
import com.e3.sysmg.security.service.SecurityService;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;

public class RoleRPCServiceImpl extends GwtRPCAction implements RoleRPCService{
	private SecurityService secService;
	private SecurityQueryService secQueryService;
	
	private SecurityService getSecService() {
		if(secService == null){
			secService = ServiceLocator.getService(SecurityService.class);
		}
		return secService;
	}

	private SecurityQueryService getSecQueryService() {
		if(secQueryService == null){
			secQueryService = ServiceLocator.getQueryService(SecurityQueryService.class);
		}
		return secQueryService;
	}
	
	public List<RoleModel> getRolesByCondition(final RoleQC roleQC) throws WebException {
		return new GwtRPCExecutor<List<RoleModel>>() {
			@Override
			public List<RoleModel> execute() throws ServiceException, Exception {
				return getSecQueryService().getRolesByCondition(roleQC);
			}
		}.doExecute();
	}

	public List<RoleModel> getAllRoles() throws WebException {
		return new GwtRPCExecutor<List<RoleModel>>() {
			@Override
			public List<RoleModel> execute() throws ServiceException, Exception {
				return getSecQueryService().getAllRoles();
			}
		}.doExecute();
	}

	public RoleModel newRole(final RoleModel roleModel) throws WebException {
		return new GwtRPCExecutor<RoleModel>() {
			@Override
			public RoleModel execute() throws ServiceException, Exception {
				return getSecService().newRole(roleModel);
			}
		}.doExecute();
	}

	public RoleModel modifyRole(final RoleModel roleModel) throws WebException {
		return new GwtRPCExecutor<RoleModel>() {
			@Override
			public RoleModel execute() throws ServiceException, Exception {
				return getSecService().modifyRole(roleModel);
			}
		}.doExecute();
	}

	public void deleteRoles(final List<RoleModel> roleModels) throws WebException {
		new GwtRPCVoidExecutor() {
			
			@Override
			public void execute() throws ServiceException, Exception {
				getSecService().deleteRoles(roleModels);
			}
		}.doExecute();
	}

	public List<ModuleModel> getAllModulesOfRole(final RoleModel roleModel)
			throws WebException {
		return new GwtRPCExecutor<List<ModuleModel>>() {
			@Override
			public List<ModuleModel> execute() throws ServiceException, Exception {
				return getSecQueryService().getAllModulesOfRole(roleModel);
			}
		}.doExecute();
	}

	public List<ModuleModel> getRoleSelfModules(final RoleModel roleModel) throws WebException{
		return new GwtRPCExecutor<List<ModuleModel>>() {
			@Override
			public List<ModuleModel> execute() throws ServiceException, Exception {
				return getSecQueryService().getRoleModules(roleModel);
			}
		}.doExecute();
		
	}
}
